<?php
class CommentAction extends Action{

	public function addComment(){//添加评论
		//$data['content'] = '@333名字 这是api测试评论111';//评论内容
		//$data['row_id'] = '68';//动态ID
		//$data['uid'] = '2';//用户ID
		//$data['to_uid'] = '5';
		$data['content'] = $_POST['content'];
		$data['row_id'] = $feed_id = $_POST['feedid'];
		$app_uid = M('Feed')->where("feed_id = $feed_id")->field("uid")->find();
		$data['app_uid'] = $app_uid['uid'];
		$data['uid'] = $_POST['uid'];
		$data['to_uid'] = $_POST['touid'];
		$data['app'] = 'public';
		$data['table'] = 'Feed';//操作表
		//dump($data);exit;
		$comment_id = model('Comment')->addComments($data,true);
		//dump($comment_id);exit;
		if(!$comment_id){//写入失败
			$value = ['code'=>'wrong'];
			header("Content-Type: text/json");
    		die(json_encode($value));
		
		}
		$notify = new NotifyAction();
		$notify->addNotify($app_uid['uid'], 'comment');
		if($_POST['touid'] && $_POST['touid'] != $app_uid['uid']){
			$notify->addNotify($_POST['touid'], 'comment');
		}
		$comment = M('Comment');
		
		$data = $comment->where("comment_id = $comment_id")->find();
		//dump($data);
		if($data){
			$value = ['code'=>'success','data'=>$data];
			header("Content-Type: text/json");
    		die(json_encode($value));
		}else{
			$value = ['code'=>'flase'];
    		die(json_encode($value));
		}
	}

	public function getAllcomment(){//获取所有评论
		$row_id = $_POST['feedid'];
//		$comment = M('Comment');
		$comment = new FeedAction();
		$allComment = $comment->getAllcomment($row_id);
		//dump($data);
		if($allComment){
			if($allComment !=null){
				foreach ($allComment as $key=>$c){
					if($c['to_uid']){
						$to_uid = $c['to_uid'];
						$user = M('User')->where("uid = $to_uid")->field("uname")->find();
						$allComment[$key]['to_uname'] = $user['uname'];
					}
						
				}
			}
			$value = ['code'=>'success','data'=>$allComment];
			//dump($value);exit;
			header("Content-Type: text/json");
    		die(json_encode($value));
		}else{
			$value = ['code'=>'flase'];
			header("Content-Type: text/json");
    		die(json_encode($value));
		}
	}

}